package day27;

import javax.swing.JPanel;
import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MapModifyPanel extends JPanel {
	private JPanel panel;
	private JTextField findStr;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private JLabel statusbar;
	private JPanel panel_1;
	private JButton button;
	private JTextField address;
	private JTextField birthday;
	private JTextField irum;
	private JTextField id;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JButton btnNewButton_1;

	/**
	 * Create the panel.
	 */
	public MapModifyPanel() {
		setLayout(new BorderLayout(0, 0));
		add(getPanel(), BorderLayout.NORTH);
		add(getStatusbar(), BorderLayout.SOUTH);
		add(getPanel_1(), BorderLayout.CENTER);

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getFindStr(), BorderLayout.CENTER);
			panel.add(getLblNewLabel(), BorderLayout.WEST);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("아이디");
		}
		return lblNewLabel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					id.setText("");
					irum.setText("");
					birthday.setText("");
					address.setText("");
					
					Data d = MapFrame.map.get(findStr.getText());
					if(d != null){
						id.setText(d.getId());
						irum.setText(d.getName());
						birthday.setText(d.getBirthday());
						address.setText(d.getAddress());
						statusbar.setText("수정 또는 삭제할 수 있습니다.");
					}else{
						statusbar.setText("자료가 없습니다.");
					}

				}
			});
		}
		return btnNewButton;
	}
	private JLabel getStatusbar() {
		if (statusbar == null) {
			statusbar = new JLabel("삭제하거나 수정할 자료를 아이디를 기준으로 검색하세요.");
			statusbar.setOpaque(true);
			statusbar.setBackground(Color.LIGHT_GRAY);
		}
		return statusbar;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.add(getButton());
			panel_1.add(getAddress());
			panel_1.add(getBirthday());
			panel_1.add(getIrum());
			panel_1.add(getId());
			panel_1.add(getLabel());
			panel_1.add(getLabel_1());
			panel_1.add(getLabel_2());
			panel_1.add(getLabel_3());
			panel_1.add(getBtnNewButton_1());
		}
		return panel_1;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int yn = JOptionPane.showConfirmDialog(MapModifyPanel.this,"정말삭제 하시려우???");
					if(yn == JOptionPane.OK_OPTION){
						MapFrame.map.remove(findStr.getText());
						statusbar.setText("삭제됨");
						id.setText("");
						irum.setText("");
						birthday.setText("");
						address.setText("");
						findStr.requestFocus();
					}else{
						statusbar.setText("취소됨");
					}
					
				}
			});
			button.setBounds(77, 116, 97, 23);
		}
		return button;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setColumns(10);
			address.setBounds(81, 85, 193, 21);
		}
		return address;
	}
	private JTextField getBirthday() {
		if (birthday == null) {
			birthday = new JTextField();
			birthday.setColumns(10);
			birthday.setBounds(81, 60, 93, 21);
		}
		return birthday;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setColumns(10);
			irum.setBounds(81, 35, 116, 21);
		}
		return irum;
	}
	private JTextField getId() {
		if (id == null) {
			id = new JTextField();
			id.setEditable(false);
			id.setColumns(10);
			id.setBounds(81, 10, 70, 21);
		}
		return id;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("아이디");
			label.setBounds(12, 13, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("성명");
			label_1.setBounds(12, 38, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("생년월일");
			label_2.setBounds(12, 63, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("주소");
			label_3.setBounds(12, 88, 57, 15);
		}
		return label_3;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Data d = new Data(id.getText(), irum.getText(), birthday.getText(), address.getText());
					MapFrame.map.remove(id.getText());
					MapFrame.map.put(id.getText(),d);
					statusbar.setText("자료가 수정되었습니다.");
				}
			});
			btnNewButton_1.setBounds(186, 116, 97, 23);
		}
		return btnNewButton_1;
	}
}
